package org.ruoyi.interview.controller;

import lombok.RequiredArgsConstructor;
import org.ruoyi.common.core.domain.R;
import org.ruoyi.common.core.domain.model.LoginUser;
import org.ruoyi.common.satoken.utils.LoginHelper;
import org.ruoyi.interview.service.impl.AIModelManager;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 面试AI控制器
 *
 * @author ruoyi
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/interview/ai")
public class InterviewAIController {

    private final AIModelManager aiModelManager;

    /**
     * 调用AI回答问题
     *
     * @param question 用户问题
     * @param model    AI模型名称（可选，默认为modelscope）
     * @return AI回答结果
     */
    @PostMapping("/ask")
    public R<String> ask(@RequestParam String question,
                         @RequestParam(required = false, defaultValue = "xunfei") String model) {
        try {
//            LoginUser loginUser = LoginHelper.getLoginUser();
//            Long userId = loginUser.getUserId();

            String answer = aiModelManager.askModel(model, question, null);
            return R.ok(answer);
        } catch (Exception e) {
            return R.fail("AI服务调用失败: " + e.getMessage());
        }
    }

    /**
     * 获取支持的AI模型列表
     *
     * @return AI模型列表
     */
    @GetMapping("/models")
    public R<List<String>> getSupportedModels() {
        return R.ok(aiModelManager.getSupportedModels());
    }

    /**
     * 清除用户对话历史
     *
     * @return 操作结果
     */
    @DeleteMapping("/history/clear")
    public R<String> clearHistory() {
        try {
            LoginUser loginUser = LoginHelper.getLoginUser();
            Long userId = loginUser.getUserId();

            aiModelManager.clearHistory(userId);
            return R.ok("对话历史已清除");
        } catch (Exception e) {
            return R.fail("清除对话历史失败: " + e.getMessage());
        }
    }
}
